<?php

namespace App\Http\Controllers\V1\Admin;

use App\Http\Controllers\V1\Controller;
use App\Models\V1\SysOperateLogs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class BaseController extends Controller
{
    public $token=null;
    public $user=null;
    public function __construct(Request $request)
    {
        $this->user=auth('box_admin')->user();

        //写入操作日志
        if($this->user){
            self::writeLog($this->user,$request->route()->getActionName(),$request->route()->getActionMethod(),"GET",auth('box_admin')->user()->username,$request->url(),$request->getClientIp(),"",json_encode($request->all(),JSON_UNESCAPED_UNICODE));
        }
    }
    /**
     * 写入操作日志
     * @param $title
     * @param $method
     * @param $request_method
     * @param $oper_name
     * @param $oper_url
     * @param $oper_ip
     * @param $oper_location
     * @param $oper_param
     * @param $json_result
     * @param $error_msg
     * @return void
     */
    protected function writeLog($user,$title,$method,$request_method,$oper_name="",$oper_url="",$oper_ip="",$oper_location,$oper_param,$json_result="",$error_msg=""){
        try {
            Log::info("日志写入");
            Log::info(json_encode($user));
            $model=new SysOperateLogs();
            $model->create([
                'admin_id'=>$user->id,
                'title'=>$title,
                'method'=>$method,
                'request_method'=>$request_method,
                'oper_name'=>$oper_name,
                'oper_url'=>$oper_url,
                'oper_ip'=>$oper_ip,
                'oper_location'=>$oper_location,
                'oper_param'=>$oper_param,
                'json_result'=>$json_result,
                'state'=>1,
                'error_msg'=>$error_msg
            ]);
        }catch (\Exception $e){
            Log::info("异常:".$e->getMessage());
        }

    }
}
